import { Component, EventTarget,_decorator } from "cc";
import BuffMgr from "../../skills/buffs/BuffMgr";
import SkillMgr from "../../skills/SkillMgr";
import PropertySet from "./PropertySet";
import Skill from "../../skills/Skill";
import BattleRoom from "./BattleRoom";
import EventTriggerType from "../../skills/triggers/SkillEventTriggerType";
import PropertyIdDefine from "./PropertyIdDefine";
const { ccclass, property } = _decorator;


@ccclass('BattleObj')
export default class BattleObj extends Component {

    id: number;//唯一id
    owerId: number;//主人id
    type: number;//类型 1:怪物 2：英雄

    //属性
    hp: number;//当前血量
    maxHp: number;//最大血量
    sp: number;//当前蓝量
    maxSp: number;//最大蓝量
    //攻击，护盾，防御，暴击，闪避等

    properties: PropertySet = new PropertySet();//运行时属性值
    baseProperties: PropertySet = new PropertySet();//基础属性值，等级对应的基础属性

    skillMgr: SkillMgr = new SkillMgr(this);//技能管理器
    buffMgr: BuffMgr = new BuffMgr();//buff管理器

    eventDispatcher: EventTarget = new EventTarget();//事件分发器

    room: BattleRoom;//所在的战斗房间，如果是地图场景，则为地图场景对象

    public get isDead() {
        return this.hp <= 0;
    }

    /**
     * 修改血量
     * @param changeHp 血量改变值
     * @param sourceObj 谁造成的此次血量改变
     * @param skill 伤害技能来源
     * @param finallyChange 是否是最终修改，不是的话可能被减免,分摊等
     * @param changeType 通过什么效果来修改的
     */
    changeHp(changeHp: number, sourceObj: BattleObj, skill: Skill, finallyChange: boolean, changeType: number) {
        if (changeHp == 0) {
            return
        }
        if (this.isDead && changeHp < 0) {//死亡无法再减血
            return
        }
        let oldDead = this.isDead;
        let oldHp = this.hp;
        let newHp = this.hp + changeHp
        if (changeHp < 0) {//减血
            // if(无敌状态){
            //     return 0;    
            // }

            this.hp = Math.max(0, newHp);
        } else {        //加血
            this.hp = Math.min(this.maxHp, newHp);
        }

        let realyChangeHp = this.hp - oldHp;
        //发布事件
        this.eventDispatcher.emit(EventTriggerType.RunTimePropertyChange, PropertyIdDefine.Hp, realyChangeHp, sourceObj, skill, changeType);
        this.room.eventDispatcher.emit(EventTriggerType.RunTimePropertyChange, PropertyIdDefine.Hp, realyChangeHp, sourceObj, skill, changeType);
        if (!oldDead && this.isDead) {//死亡
            this.room.eventDispatcher.emit(EventTriggerType.DeadBefore, this);
        }
        return realyChangeHp;
    }

    releaseSkill(){
        this.skillMgr.releaseSkill();
    }
}


